"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : security-user
Case Name   : 修改普通用户为监控管理员用户，并进行DDL操作
Description :
    1.创建普通用户
    CREEATE USER monadmin WITH  PASSWORD '$PASSWORD'
    2.修改用户为监控管理员用户
    ALTER USER monadmin MONADMIN;
    3.监控管理员用户登录，执行DDL语句
    CREATE TABLE table001(id INT);
    INSERT INTO table001 VALUES(3);
    DROP TABLE table001;
Expect      :
    1.CREATE ROLE
    2.ALTER ROLE
    3.CREATE TABLE
    INSERT 3
    DROP TABLE
History     :
"""
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common


class Privategrant(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(
            '------Opengauss_Function_Security_User_Case0011 start-----')
        self.userNode = Node('PrimaryDbUser')
        self.DB_ENV_PATH = macro.DB_ENV_PATH
        self.sh_primy = CommonSH('PrimaryDbUser')
        self.common = Common()

    def test_default_permission(self):
        self.logger.info('---------创建普通用户-------------')
        sql_cmd1 = f'CREATE USER monadmin WITH PASSWORD ' \
                   f'\'{macro.COMMON_PASSWD}\';' \
                   f'ALTER USER monadmin MONADMIN;'
        self.logger.info(sql_cmd1)
        msg1 = self.sh_primy.execut_db_sql(sql_cmd1)
        self.logger.info(msg1)
        self.assertTrue('CREATE ROLE' in msg1 and 'ALTER ROLE' in msg1)
        self.logger.info('---------MONADMIN用户执行DDL语句------------')
        sql_cmd2 = 'CREATE TABLE table001(id INT);' \
                   'INSERT INTO table001 VALUES(3);' \
                   'DROP TABLE table001;'
        excute_cmd2 = f'source {self.DB_ENV_PATH};' \
                      f'gsql -d {self.userNode.db_name} -p ' \
                      f'{self.userNode.db_port} -U monadmin -W ' \
                      f'{macro.COMMON_PASSWD} -c "{sql_cmd2}"'
        self.logger.info(excute_cmd2)
        msg2 = self.userNode.sh(excute_cmd2).result()
        self.logger.info(msg2)
        self.common.equal_sql_mdg(msg2, 'CREATE TABLE', 'INSERT 0 1',
                                  'DROP TABLE')

    def tearDown(self):
        self.logger.info('---------清理环境-----------')
        sql_cmd1 = f'drop user monadmin;'
        msg1 = self.sh_primy.execut_db_sql(sql_cmd1)
        self.logger.info(msg1)
        self.assertTrue(msg1.find('DROP ROLE') > -1)
        self.logger.info(
            '----Opengauss_Function_Security_User_Case0011 finish----')
